package com.btkjgc.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.ModelAndView;

import com.btkjgc.models.AdminUser;
import com.btkjgc.models.AjaxObj;
import com.btkjgc.models.ConventionalPostage;
import com.btkjgc.models.extend.ConventionalPostageModel;
import com.btkjgc.service.intf.ConventionalFareManageService_Intf;
import com.btkjgc.utils.HibernateUtil;

@Service
public class ConventionalFareManageService_Impl extends BaseService_Impl implements ConventionalFareManageService_Intf{
	
	static Logger logger = Logger.getLogger(ConventionalFareManageService_Impl.class);

	@Override
	public void setListData(HttpServletRequest request, ModelAndView mv,int pageIndex, int totalRecord,
			List<ConventionalPostageModel> conventionalPostageList) {
		// TODO Auto-generated method stub
		
		Enumeration<String> names = request.getParameterNames();
		while (names.hasMoreElements())
		{
			String name = names.nextElement();
			mv.addObject(name, request.getParameter(name));
		}
		
		mv.addObject("conventional_postages_list",conventionalPostageList);
 
		
		mv.addObject("currentpage", pageIndex);
		mv.addObject("totalRecord", totalRecord);
		
	}


	@Override
	public AjaxObj insertConventionalPostageModel(
			ConventionalPostageModel conventionalPostageModel) {
		// TODO Auto-generated method stub
		
		AjaxObj ajaxObj=new AjaxObj();
		ajaxObj.setResult(true);
		
		try {
			//保存计费规则
			if(conventionalPostageModel.getAreaRadio()==1){
				conventionalPostageModel.setProvinceids(null);
				conventionalPostageModel.setCityids(null);
			}
			if(conventionalPostageModel.getBeginTimeRadio()==1){
				conventionalPostageModel.setBeginTime(null);
			}
			if(conventionalPostageModel.getEndTimeRadio()==1){
				conventionalPostageModel.setEndTime(null);
			}
	
			
			ConventionalPostage conventionalPostage  =new ConventionalPostage();
			PropertyUtils.copyProperties(conventionalPostage,conventionalPostageModel);
			conventionalPostage.setDel(0);
			conventionalPostage.setCreatedate(new Date());
			conventionalPostage.setStatus(1);
			conventionalPostage.setOperdate(new Date());
			conventionalPostage.setOperid(getLoginUser().getId());
			HibernateUtil.save(conventionalPostage);
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			ajaxObj.setResult(false);
			ajaxObj.setMsg("添加时出错");
		}
		
		return ajaxObj;
	}

	@Override
	public AjaxObj updateConventionalPostageModel(
			ConventionalPostageModel conventionalPostageModel) {
		// TODO Auto-generated method stub
		
		AjaxObj ajaxObj=new AjaxObj();
		ajaxObj.setResult(true);
		//手动控制事务批量提交
		Session session =HibernateUtil.openSession();
		Transaction tx  = session.beginTransaction();
		
		ConventionalPostage  conventionalPostage=new ConventionalPostage();
		if(conventionalPostageModel.getAreaRadio()==1){
			session.createQuery("update from ConventionalPostage set provinceids=null where id="+conventionalPostageModel.getId()).executeUpdate();
			session.createQuery("update from ConventionalPostage set cityids=null where id="+conventionalPostageModel.getId()).executeUpdate();
			conventionalPostageModel.setProvinceids(null);
			conventionalPostageModel.setCityids(null);
		}
		if(conventionalPostageModel.getBeginTimeRadio()==1){
			session.createQuery("update from ConventionalPostage set begintime=null where id="+conventionalPostageModel.getId()).executeUpdate();
			conventionalPostageModel.setBeginTime(null);
		}
		if(conventionalPostageModel.getEndTimeRadio()==1){
			session.createQuery("update from ConventionalPostage set endtime=null where id="+conventionalPostageModel.getId()).executeUpdate();
			conventionalPostageModel.setEndTime(null);
		}
		conventionalPostageModel.setOperdate(new Date());
		conventionalPostage.setOperid(getLoginUser().getId());
		session.flush();
		session.clear();
		try {
			PropertyUtils.copyProperties(conventionalPostage, conventionalPostageModel);
			session.createQuery(getUpdateHql(conventionalPostage)).executeUpdate();
			tx.commit();
 
		} catch (IllegalAccessException | InvocationTargetException
				| NoSuchMethodException e) {
			// TODO Auto-generated catch block
			ajaxObj.setResult(false);
			ajaxObj.setMsg("修改时出错");
			e.printStackTrace();
		}
		
		return ajaxObj;
	}

	@Override
	public void setEditdata(HttpServletRequest request, ModelAndView mv,
			Integer id) {
		// TODO Auto-generated method stub
		Enumeration<String> names = request.getParameterNames();
		while (names.hasMoreElements()) {
			String name = names.nextElement();
			mv.addObject(name, request.getParameter(name));
		}
		
    	ConventionalPostageModel conventionalPostageModel=(ConventionalPostageModel) HibernateUtil.executeSQLQueryObject("select * from conventional_postage_t where id ="+id,ConventionalPostageModel.class);
		
		if(StringUtils.isEmpty(conventionalPostageModel.getBeginTime())){
			conventionalPostageModel.setBeginTimeRadio(1);
		}else {
			conventionalPostageModel.setBeginTimeRadio(2);
		}
		if(StringUtils.isEmpty(conventionalPostageModel.getEndTime())){
			conventionalPostageModel.setEndTimeRadio(1);
		}else {
			conventionalPostageModel.setEndTimeRadio(2);
		}
		
		if(StringUtils.isEmpty(conventionalPostageModel.getProvinceids())&&StringUtils.isEmpty(conventionalPostageModel.getCityids())){
			conventionalPostageModel.setAreaRadio(1);
		}else {
			conventionalPostageModel.setAreaRadio(2);
		}
		
		mv.addObject("conventionalPostageModel", conventionalPostageModel);
		
	}
	
	@Override
	public void updateStateByIds(String idStr,Integer state) 
	{
		AdminUser adminUser=getLoginUser();
		String hql = "update   conventional_postage_t set status= ?,operdate=now(),operid="+adminUser.getId()+"  where id in( "+idStr+")";
		HibernateUtil.executeSQLUpdate(hql, state.toString());
		
	}
	

	@Override
	public void updateDelByIds(String idStr) {
		// TODO Auto-generated method stub
		AdminUser adminUser=getLoginUser();
		String hql = "update  conventional_postage_t set del=1,operdate=now(),operid="+adminUser.getId()+" where id in( ? )";
		HibernateUtil.executeSQLUpdate(hql, idStr);
	}

}